/* Copyright 2011 Elektrobit Wireless Communications Oy (EB)


  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 2 as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


 *All rights reserved 
 *Version:Elektrobit_Qt_1.0.0_20110305 */


#include "servicemanager.h"
#include <appcommon.h>
#include <libs/propertyhelper.h>
#include <libs/xmlreader.h>
#include <QHashIterator>
#include <QMetaObject>
#include <QMetaType>
#include <QVariant>


ServiceManager *ServiceManager::manager = NULL;

ServiceManager *ServiceManager::instance() {
    if(manager == NULL) {
        manager = new ServiceManager();
    }
    return manager;
}

ServiceManager::ServiceManager(QObject *parent) : QObject(parent) {
    
}

void ServiceManager::start() {
    loadServices();
    startServices();

    emit this->servicesStarted();
}

void ServiceManager::init(const QString &configFile) {
    this->mConfigFile = configFile;
}

Service *ServiceManager::getService(const QString &serviceName) {
    return this->mServices.value(serviceName, NULL);
}

void ServiceManager::startServices() {
    for(int i = 0; i < mServicesList.size(); i++) {
        if(!startService(mServicesList.value(i, NULL))) {
            
        } else {
            APPERROR("service  start error");
            break;            
        }
    }
}

int ServiceManager::startService(Service *service) {
    if(service == NULL) {
        return -1;
    }
    APPINFO << service;
    return service->start();
}

void ServiceManager::loadServices() {
    this->loadBaseServices();
    this->loadAppServices();
}

void ServiceManager::loadBaseServices() {
    
}

void ServiceManager::loadAppServices() {
    QDomDocument doc;
    if(!XmlReader::getXmlDocument(this->mConfigFile, doc)) {
        QDomElement node = doc.documentElement().firstChildElement("service");
        while( !node.isNull() ) {
            Service *s = createServiceByType(node.firstChildElement("class_name").text());
            if(s) {
                PropertyHelper::bindProperties(s, node.firstChildElement("properties"));
                mServices.insert(node.firstChildElement("class_name").text(), s);
                mServicesList.append(s);
            }
            node = node.nextSiblingElement();
        }
    }
}

Service *ServiceManager::createServiceByType(const QString &type) {
    int id = QMetaType::type(type.toUtf8().data());
    APPINFO << QMetaType::typeName(id) << id;
    Service *s = NULL;
    if(id) {
        void *service = QMetaType::construct(id);
        s = static_cast<Service *>(service);
    } else {
        APPWARAING << "service " << type << " create error";
    }
    return s;
}

